package com.duobao.view;
import java.awt.*;
import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;
import com.duobao.wuping.*;

import java.awt.event.*;
import java.lang.reflect.*;

public class Diuqihuobi extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    Equip equip;
    JLabel fanhui;
    JLabel fanhuiyouxi;

    JLabel tishi;
    JTextField jtf;
    JLabel queding;
    public Diuqihuobi(DuobaoSwingLaunch game, Equip equip)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);
        this.equip = equip;


        setTishi();
        setJtf();
        setQueding();


        setFanhui();
        setFanhuiyouxi();
    }
    private void setXiaoguo()
    {

    }
    private void setTishi() {
        tishi=new JLabel("你要扔掉多少"+equip.getName()+"？");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setJtf() {
        jtf = new JTextField(20);
        jtf.setFont(myFont.f3);
        JPanel jpn2 = new JPanel();
        jpn2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jpn2.add(jtf);
        this.add(jpn2);
    }
    private void setQueding() {
        queding=new JLabel();
        queding.setFont(myFont.f2);
        queding.addMouseListener(this);
        queding.setForeground(Color.BLUE);
        queding.setText("<html><u>"+"确定"+"</u></html>");
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(queding);
        this.add(jp1);
    }
    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }

    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==tishi)
        {
            f=true;
        }
        return f;
    }
    private void quedinggongneng()
    {
        int value;

        try {
            value=Integer.parseInt(this.jtf.getText());
        } catch (NumberFormatException e1) {
            this.jtf.setText("");
            return ;
        }
        if(this.equip.getCategory()==10)
        {
            if(value>this.equip.getJZH() || value<=0)
            {
                return ;
            }else if(value==this.equip.getJZH())
            {
                this.game.hero.getEquip2().remove(this.equip);
                this.game.setPanel(new Diuqi(this.game,this.equip));

            }else
            {
                this.equip.setJZH(this.equip.getJZH()-value);
                this.equip.setFZ(this.equip.getJZH()/10);
                this.game.setPanel(new Diuqi(this.game,new Huangjing(value)));

            }
        }else if(this.equip.getCategory()==11)
        {
            if(value>this.equip.getJZB() || value<=0)
            {
                return ;
            }else if(value==this.equip.getJZB())
            {
                this.game.hero.getEquip2().remove(this.equip);
                this.game.setPanel(new Diuqi(this.game,this.equip));

            }else
            {
                this.equip.setJZB(this.equip.getJZB()-value);
                this.equip.setFZ(this.equip.getJZB()/10);
                this.game.setPanel(new Diuqi(this.game,new Baiying(value)));

            }
        }else if(this.equip.getCategory()==12)
        {
            if(value>this.equip.getJZT() || value<=0)
            {
                return ;
            }else if(value==this.equip.getJZT())
            {
                this.game.hero.getEquip2().remove(this.equip);
                this.game.setPanel(new Diuqi(this.game,this.equip));

            }else
            {
                this.equip.setJZT(this.equip.getJZT()-value);
                this.equip.setFZ(this.equip.getJZT()/10);
                this.game.setPanel(new Diuqi(this.game,new Baiying(value)));

            }
        }else
        {
            System.out.println("警告：不是货币");
        }
    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==queding)
        {
            this.quedinggongneng();
        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            try {
                this.game.setPanel(new Shuishen(this.game,ZhuangTaiPanel.class.getDeclaredConstructor(DuobaoSwingLaunch.class,Constructor.class,int.class),1));
            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
